﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EQual.MainModule.Documents.Models;
using EQual.MainModule.Documents.Views;
using EQual.MainModule.Documents.DocumentsDetailViews ;
using Microsoft.Practices.Composite.Regions;
using EQual.Infrastructure;

namespace EQual.MainModule.Documents.Presenters
{
   public class DocumentsController :  EQual.MainModule.Documents.Presenters.IDocumentsController, EQual .Infrastructure .IModuleController 
    {
        //Data Declerations 
        DataAccesLayer.DocumentDataAccesLayer accesLayer;

        public DataAccesLayer.DocumentDataAccesLayer AccesLayer
        {
            get { return accesLayer; }
            set { accesLayer = value; }
        }

       IRegionManager _regionManager;

       IDocumentsPresentationModel ViewModel;

       IDocumentGridView _view_Main;

       public IDocumentGridView View
       {
           get { return _view_Main; }
           set { _view_Main = value; }
       }

       public DocumentsController(IRegionManager regionManager):this()
       {
           this._regionManager = regionManager;
       }

       public DocumentsController( )
       {
           accesLayer = new DataAccesLayer.DocumentDataAccesLayer();
       }

       //secondary Presenters and Views 
       DocumentsDestributionPresenter presenter_distrebution;
       DocumentDetailedControlPresenter preseter_details;
       DetailViews.RoleDestributionPresenter RoleDestribution;
       VersionsPresenter VersionController = new VersionsPresenter();

       public void Run()
       {
           ViewModel = new EQual.MainModule.Documents.Models.DocumentsPresentationModel();
           ViewModel.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(ViewModel_PropertyChanged);

           View = new EQual.MainModule.Documents.Views.DocumentsGridViewControl();
           
           ViewModel.View = View;
           View.Model =ViewModel;
           //bind main view Events 
           View.ItemSelected  += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<DataAccesLayer.Document>>(View_DocumentSelected);
           View.ItemAdded  += new EventHandler<Microsoft.Practices.Composite.Events.DataEventArgs<DataAccesLayer.Document>>(View_Main_DocumentAdded);

           presenter_distrebution = new DocumentsDestributionPresenter();
           preseter_details = new DocumentDetailedControlPresenter();
           RoleDestribution = new EQual.MainModule.Documents.DetailViews.RoleDestributionPresenter();

           //Bind Document Save View Events
           preseter_details.View.ItemSaved += VersionController.DocumentSavedHandler();
           View.ItemSaved += VersionController.DocumentSavedHandler();

           //inject View 
           this._regionManager.RegisterViewWithRegion(RegionNames.MainRegion,() => this.View);
           this._regionManager.Regions [RegionNames.MainRegion].Activate (this.View);
       }

       void ViewModel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
       {
           if (e.PropertyName == "ActiveItem")
           {
               DispalyItem(ViewModel.ActiveItem);
           }
       }

       void View_Main_DocumentAdded(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<DataAccesLayer.Document> e)
       {
           this.RequestNewDocumentCreation(e.Value);
       }

       DetailsContainer container = new DetailsContainer();

       void View_DocumentSelected(object sender, Microsoft.Practices.Composite.Events.DataEventArgs<DataAccesLayer.Document> e)
       {
           DispalyItem(e.Value);
       }

       private void DispalyItem(DataAccesLayer.Document doc)
       {
           InjectViews();

           presenter_distrebution.DisplayDocumentDestribution(doc);
           preseter_details.DisplayDocument(doc);
           RoleDestribution.ActiveItem =doc;
           VersionController.ActiveItem = doc;
       }

       protected virtual void InjectViews()
       {
           //throw in to the datails region the datails view
           IRegion region = _regionManager.Regions[RegionNames.ActionRegion];

           if (!region.Views.Contains(container))
           {
               container.AddView(LabelDecorateView(preseter_details.View, "Λεπτομέρειες"));
               container.AddView(LabelDecorateView(presenter_distrebution.View, "Διανομή"));
             //  container.AddView(LabelDecorateView( presenter_forms.View, "Εντυπα"));
               container.AddView(RoleDestribution.View);
               region.Add(container);
           }
           //activate the current view
           region.Activate(container);
       }

       public object LabelDecorateView(object view, string header)
       {
           return new DataGridDecorator(view, header);
       }

       public void SubmitChanges()
       {
           AccesLayer.SubmitChanges();
       }

       public void RequestNewDocumentCreation(DataAccesLayer.Document document)
       {
           try
           {
               if (AccesLayer.DoesDocumentExist(document))
               {
                   AccesLayer.UpdateDocument(document);
               }
               else
               {
                   AccesLayer.AddNewDocument(document);
               }
           }
           catch (Exception e)
           {

           }

       }

       public void RequestDocumentDelete(DataAccesLayer.Document doc)
       {
           AccesLayer.DeleteDocument(doc);
       }

       public  void RequestDocumentDelete(System.Collections.IList iList)
       {
           if (iList == null) return;

           foreach (DataAccesLayer.Document doc in iList)
           { RequestDocumentDelete(doc); }
       }

    }
}
